﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using InHD.Properties;

namespace InHD.model
{
    class Customer : IDataErrorInfo
    {
        #region Creation

        public static Customer CreateCustomer()
        {
            return new Customer();
        }

        public static Customer CreateCustomerInfo(
            string code,
            string fname,
            string lname,
            string company,
            string position,
            string phone,
            string fax,
            string email,
            string address,
            string unk,
            double totalBought,
            DateTime lastAccess)
        {
            return new Customer
            {
                CustomerCode = code,
                LastName = lname,
                FirstName = fname,
                Company = company,
                Position = position,
                Phone = phone,
                Fax = fax,
                Email = email,
                Address = address,
                CustomerUNK = unk,
                TotalBought = totalBought,
                LastAccess = lastAccess
            };
        }

        protected Customer()
        {
        }

        #endregion // Creation

        #region State Properties

        public string CustomerCode { get; set; }

        public string LastName { get; set; }

        public string FirstName { get; set; }

        public string Company { get; set; }
        
        public string Position { get; set; }

        public string Phone { get; set; }

        public string Fax { get; set; }

        public string Email { get; set; }
        
        public string Address { get; set; }

        public string CustomerUNK { get; set; }

        public double TotalBought { get; set; }

        public DateTime LastAccess { get; set; }

        public static Dictionary<string, string> Fields
        {
            get
            {
                Dictionary<string, string> fields = new Dictionary<string, string>();
                fields.Add("FirstName", "Tên");
                fields.Add("LastName", "Họ");
                fields.Add("Company", "Công ti");
                fields.Add("Position", "Chức vụ");
                fields.Add("Phone", "Điện thoại");
                fields.Add("Fax", "Fax");
                fields.Add("Email", "Email");
                fields.Add("Address", "Địa chỉ");
                return fields;
            }
        }

        #endregion // State Properties

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "FirstName",
            "LastName",
            "Phone",
            "Fax",
            "Email",
            "CustomerCode"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "FirstName":
                    error = this.ValidateFirstName();
                    break;

                case "LastName":
                    error = this.ValidateLastName();
                    break;

                case "Phone":
                    error = this.ValidatePhone();
                    break;

                case "Fax":
                    error = this.ValidateFax();
                    break;

                case "Email":
                    error = this.ValidateEmail();
                    break;

                case "CustomerCode":
                    error = this.ValidateCustomerCode();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidateEmail()
        {
            if (!utilities.ValidatingUtilities.ValidateMissing(this.Email))
            {
                if (utilities.ValidatingUtilities.ValidateEmail(this.Email))
                {
                    return Resources.InHD_InvalidEmail;
                }
            }

            return null;
        }

        private string ValidateFax()
        {
            if (!utilities.ValidatingUtilities.ValidateMissing(this.Fax))
            {
                if (utilities.ValidatingUtilities.ValidatePhone(this.Fax))
                {
                    return Resources.InHD_InvalidPhone;
                }
            }

            return null;
        }

        private string ValidatePhone()
        {
            if (!utilities.ValidatingUtilities.ValidateMissing(this.Phone))
            {
                if (utilities.ValidatingUtilities.ValidatePhone(this.Phone))
                {
                    return Resources.InHD_InvalidPhone;
                }
            }

            return null;
        }

        string ValidateFirstName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.FirstName))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidateLastName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.LastName))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        string ValidateCustomerCode()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.CustomerCode))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateUsername(this.CustomerCode))
                {
                    return Resources.InHD_InvalidUsername;
                }
            }
            return null;
        }

        #endregion // Validation

        public Customer CreateShallowCopy()
        {
            return (Customer)this.MemberwiseClone();
        }
    }
}
